package com.example.baserecyclerviewadapterdemo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.animation.ScaleInAnimation;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.adapter.HeaderAndFooterAdapter;
import com.example.baserecyclerviewadapterdemo.data.DataServer;

public class HeaderAndFooterUseActivity extends AppCompatActivity {

    public static final int PAGE_SIZE = 3;
    private HeaderAndFooterAdapter mAdapter;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_universal_recycler);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new HeaderAndFooterAdapter(DataServer.getSampleData(PAGE_SIZE));
        mAdapter.setAnimationEnable(false);


        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Toast.makeText(HeaderAndFooterUseActivity.this, "您选择了：" + position, Toast.LENGTH_SHORT).show();
            }
        });

        View headerView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addHeaderView(getHeaderView(1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAdapter.removeHeaderView(v);
                    }
                }),0);
            }
        });
        mAdapter.addHeaderView(headerView,0);

        View footerView = getFooterView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.addFooterView(getFooterView(1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAdapter.removeFooterView(v);
                    }
                }),0);
            }
        });
        mAdapter.addFooterView(footerView,0);

        recyclerView.setAdapter(mAdapter);
    }

    private View getHeaderView(int type,View.OnClickListener listener){
        View view = getLayoutInflater().inflate(R.layout.layout_head_view,recyclerView,false);
        if (type == 1) {
            ImageView imageView = view.findViewById(R.id.iv);
            imageView.setImageResource(R.mipmap.rm_icon);
        }
        view.setOnClickListener(listener);
        return view;
    }

    private View getFooterView(int type,View.OnClickListener listener){
        View view = getLayoutInflater().inflate(R.layout.layout_footer_view,recyclerView,false);
        if (type == 1) {
            ImageView imageView = view.findViewById(R.id.iv);
            imageView.setImageResource(R.mipmap.rm_icon);
        }
        view.setOnClickListener(listener);
        return view;
    }
}
